package com.sunny.ynhz.ui.patrol.problems.draft;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.sunny.ynhz.R;
import com.sunny.ynhz.adapter.ProblemDraftAdapter;
import com.sunny.ynhz.bean.ProBean;
import com.sunny.ynhz.db.RealmUtils;
import com.sunny.ynhz.ui.base.AppBaseTranslationTitleBarActivity;
import com.sunny.ynhz.utils.SPUtil;

import java.util.LinkedList;

import butterknife.BindView;
import io.realm.Realm;
import io.realm.RealmChangeListener;
import io.realm.RealmResults;

/**
 * Created by luao on 2017/7/24.
 */

public class ProblemDraftActivity extends AppBaseTranslationTitleBarActivity {

    @BindView(R.id.problem_draft_recycle)
    RecyclerView problemDraftRecycle;
    private ProblemDraftAdapter problemDraftAdapter;
    private LinkedList<ProBean> dataBeenList;
    private Realm mRealm;
    private Activity activity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_problem_draft);
        activity = this;
        init();
    }


    private void init() {

        dataBeenList = new LinkedList<>();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        problemDraftRecycle.setLayoutManager(linearLayoutManager);
        problemDraftAdapter = new ProblemDraftAdapter(dataBeenList);
        problemDraftRecycle.setAdapter(problemDraftAdapter);


        mRealm = RealmUtils.getDeleteRealm();
        final RealmResults<ProBean> probelmList = mRealm
                .where(ProBean.class)
                .equalTo("userId", SPUtil.get("userId", "null").toString())
                .findAllAsync();
        probelmList.addChangeListener(new RealmChangeListener<RealmResults<ProBean>>() {
            @Override
            public void onChange(RealmResults<ProBean> proBeen) {
                dataBeenList.addAll(probelmList);
                Log.e("list", "dataBeenList = " + dataBeenList.toString());
                problemDraftAdapter.notifyDataSetChanged();

            }
        });

        problemDraftAdapter.setOnItemClickListener(new ProblemDraftAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, ProBean itemData, int position) {
                Intent intent = new Intent(activity, ProblemDraftDetailActivity.class);
                intent.putExtra("id", itemData.getId());
                activity.startActivityForResult(intent, 100);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mRealm != null) {
            mRealm.close();
        }
    }
}
